﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Friend
{
    public partial class ListMain : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            int userID = int.Parse(strUserIdFromCookie);

            //页面初始化
            StringBuilder sbHtml = new StringBuilder(Cache["user_friendlist_main"].ToString());//最终写出页面的大sb
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            //------------------------------------------------------------------------
            sbHtml = sbHtml.Replace("$FriendClassOption$", FriendClassSelect(userID));  //好友分类选择下拉表
            sbHtml = sbHtml.Replace("$FriendCount$", FriendCount(userID));//统计用户的好友数量
            string strClassID = Request.QueryString["classID"];
            sbHtml = sbHtml.Replace("$user_friendlist_main_ul$",FriendList(userID,strClassID));//好友列表
            sbHtml = sbHtml.Replace("$user_friendlist_fgroup_li$", FriendClassList(userID));//好友分类列表
            sbHtml = sbHtml.Replace("$ClassNull$",new Jy123.BLL.User.Friend().CountFrdClassNull(userID).ToString());//空分类的好友数量
            sbHtml = sbHtml.Replace("$AreaNull$", new Jy123.BLL.User.Friend().CountFrdAreaNull(userID).ToString());//未知地区的好友数量
            sbHtml = sbHtml.Replace("$FriendGroupByArea$",FriendClassListByArea(userID));
            
            //导航更改
            sbHtml = sbHtml.Replace("$ClassFrdList$","class=sel")
                .Replace("$ClassMng$","")
                .Replace("$ClassOnline$", "")
                .Replace("$ClassCongnition$", "")
                .Replace("$ClassIn$", "")
                .Replace("$ClassRandom$", "")
                .Replace("$ClassOut$", "")
                .Replace("$ClassInvite$","");
            Response.Write(sbHtml.ToString());
        }
        /// <summary>
        /// 好友分类选择下拉表
        /// </summary>
        /// <param name="userID">当前用户的ID</param>
        /// <returns>字符串</returns>
        private string FriendClassSelect(int userID)
        {            
            //好友分类选择下拉表
            StringBuilder sbFriendSelect = new StringBuilder();
            string htmFriendSelect = Cache["user_friendclass_select_option"].ToString();
            List<Jy123.Model.User_FriendClass> listFriendClass = new Jy123.BLL.User_FriendClass().GetModelList("FUser='"+userID+"'"); 
            foreach (Jy123.Model.User_FriendClass frdC in listFriendClass)
            {
                List<int?> count;
                string strFrdSel = htmFriendSelect;
                strFrdSel = strFrdSel.Replace("$CID$",frdC.ID.ToString());
                strFrdSel = strFrdSel.Replace("$CName$", frdC.CName);
                DataTable dtFriendList = new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID,frdC.ID,out count).Tables[0];
                //int? frdCount = count[0];
                int? frdCount = count[0];
                strFrdSel = strFrdSel.Replace("$FrdCount$", frdCount.ToString());
                sbFriendSelect.Append(strFrdSel);
            }
            return sbFriendSelect.ToString();
        }
        /// <summary>
        /// 好友列表
        /// </summary>
        /// <param name="userID">>当前用户的ID</param>
        /// <param name="strFriendClassID">好友分类(无分类则填string.empty)</param>
        /// <returns>字符串</returns>
        private string FriendList(int userID,string strFriendClassID)
        {
            List<int?> count;
            StringBuilder sbFrdList = new StringBuilder();
            string strFrdList = Cache["user_friendlist_main_ul"].ToString();
            DataTable dtFrdList;
            if (!string.IsNullOrEmpty(strFriendClassID))
            {
                dtFrdList = new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID,Convert.ToInt32(strFriendClassID), out count).Tables[0];                
            }
            else
            {
                dtFrdList = new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, out count).Tables[0];
            }
            for (int i = 0; i < dtFrdList.Rows.Count; i++)
            {
                string str = strFrdList;
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtFrdList.Rows[i]["FFriend"]));

                str = str.Replace("$FriendUserID$", dtFrdList.Rows[i]["FFriend"].ToString());
                str = str.Replace("$FriendUserName$", dtFrdList.Rows[i]["UserName"].ToString());
                str = str.Replace("$FriendPortrait$", userPortrait);
                sbFrdList.Append(str);
            }
            return sbFrdList.ToString();
        }
        /// <summary>
        /// 统计好友数量
        /// </summary>
        /// <param name="userID">当前用户的ID</param>
        /// <returns></returns>
        private string FriendCount(int userID)
        {
            List<int?> count;
            new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, out count);
            return count[0].ToString();
        }
        /// <summary>
        /// 好友分类表
        /// </summary>
        /// <param name="userID">当前用户的ID</param>
        /// <returns></returns>
        private string FriendClassList(int userID)
        {
            //
            StringBuilder sbFriendSelect = new StringBuilder();
            string htmFriendSelect = Cache["user_friendlist_fgroup_li"].ToString();
            List<Jy123.Model.User_FriendClass> listFriendClass = new Jy123.BLL.User_FriendClass().GetModelList("FUser='" + userID + "'");
            foreach (Jy123.Model.User_FriendClass frdC in listFriendClass)
            {
                List<int?> count;
                string strFrdSel = htmFriendSelect;
                strFrdSel = strFrdSel.Replace("$CID$", frdC.ID.ToString());
                strFrdSel = strFrdSel.Replace("$CName$", frdC.CName);
                DataTable dtFriendList = new BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, frdC.ID, out count).Tables[0];
                //int? frdCount = count[0];
                int? frdCount = count[0];
                strFrdSel = strFrdSel.Replace("$FrdCount$", frdCount.ToString());
                sbFriendSelect.Append(strFrdSel);
            }
            return sbFriendSelect.ToString();
        }
        /// <summary>
        /// 好友分类表(按地区分类)
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>字符串</returns>
        private string FriendClassListByArea(int userID)
        {
            List<int?> count;
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = Cache["user_friendlist_group_by_Area"].ToString();
            DataTable dtFriendListByArea = new Jy123.BLL.User.Friend().FriendInAreaList(userID).Tables[0];
            for (int i = 0; i < dtFriendListByArea.Rows.Count; i++)
            {
                string str = strHtml;
                str = str.Replace("$AreaID$",dtFriendListByArea.Rows[i]["Province"].ToString());
                str = str.Replace("$ProvinceName$",dtFriendListByArea.Rows[i]["Name"].ToString());
                str = str.Replace("$Counts$", dtFriendListByArea.Rows[i]["Counts"].ToString());
                sbHtml.Append(str);
            }
            return sbHtml.ToString();
        }
    }
}
